(define (number-leaves bintree)
    (number-leaves-aux 0 bintree)
)

(define (number-leaves-aux leaves bintree)
    (if (integer? bintree)
        leaves
        (append
            (list (car bintree))
            (list 
                (number-leaves-aux 
                    leaves 
                    (cadr bintree)
                )
            )
            (list 
                (number-leaves-aux 
                    (+ leaves (count-leaves (cadr bintree)))
                    (caddr bintree)
                )
            )
        )
    )
)

(define (count-leaves bintree)
    (if (integer? bintree)
        1
        (+
            (count-leaves (cadr bintree))
            (count-leaves (caddr bintree))
        )
    )
)

(display
    (number-leaves
        `(
            foo
            (
                bar 
                10
                11
            )
            (
                baz
                12
                (
                    quux 
                    13 
                    14
                )
            )
        )
    )
)
